package com.iemes.controller.equip;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.DepartmentForMap;
import com.iemes.entity.EquipAccepFormMap;
import com.iemes.entity.EquipApplyFormMap;
import com.iemes.entity.EquipCategoryFormMap;
import com.iemes.entity.EquipFileFormMap;
import com.iemes.entity.ErpEquipmentapply;
import com.iemes.entity.MetalsBomFormMap;
import com.iemes.entity.RepairappFormMap;
import com.iemes.entity.ResFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.equip.EquipCategoryMapper;
import com.iemes.mapper.equip.EquipFileMapper;
import com.iemes.mapper.equip.RepairappMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@Controller
@RequestMapping("equip")
public class RepairappController extends BaseController {
	@Inject
	private RepairappMapper repairapp;
	@Inject
	private EquipFileMapper filemapper;
	@Inject
	private EquipCategoryMapper categorymapper;

	@RequestMapping(value = "equip_repairapp", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String examinepru(Model model) {
		List<RepairappFormMap> findByWhere = repairapp
				.findByWhere(new RepairappFormMap());
		List<EquipCategoryFormMap> category = categorymapper
				.findByWhere(new EquipCategoryFormMap());
		List<ResFormMap> findByRes = findByRes();
		model.addAttribute("equipbom", findByWhere);
		model.addAttribute("res", findByRes);
		model.addAttribute("category", category);
		return Common.BACKGROUND_PATH + "/equip/repairapp/equipbom_list";
	}

	@ResponseBody
	@RequestMapping("equip_repairappByPage")
	public PageView findByPage(String pageNow, String pageSize) {
		RepairappFormMap repairappFormMap = getFormMap(RepairappFormMap.class);
		if (repairappFormMap.get("status")==null) {
			repairappFormMap.put("status", 1);
		}
		List<Map<String, Object>> equipRepairs = repairapp
				.getEquipRepairs(repairappFormMap);
		try {
			LuogicPaging(pageNow, pageSize, equipRepairs);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pageView;
	
	}

	@ResponseBody
	@RequestMapping(value = "repairapp_editEntity")
	@SystemLog(module = "设备台账单", methods = "台账单-修改台账单")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		RepairappFormMap repairappFormMap = getFormMap(RepairappFormMap.class);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		repairappFormMap.set("date", sdf.format(date));
		repairappFormMap.set("confirmPeopleId", userFormMap.get("id"));
		repairapp.editEntity(repairappFormMap);
		return "success";
	}

	@RequestMapping("repair_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		List<EquipCategoryFormMap> categoryFormMap = categorymapper
				.findByWhere(new EquipCategoryFormMap());
		RepairappFormMap equipfile = repairapp.findbyFrist("id", id,
				RepairappFormMap.class);
		model.addAttribute("equipfile", equipfile);
		model.addAttribute("categoryFormMap", categoryFormMap);
		return Common.BACKGROUND_PATH + "/equip/repairapp/equipbom_edit";
	}

	
	@ResponseBody
	@RequestMapping(value = "repairapp_deleteEntity")
	@SystemLog(module = "设备台账单", methods = "设备-删除台账单")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		RepairappFormMap repairappFormMap = getFormMap(RepairappFormMap.class);
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		for (int i = 0; i < str.length; i++) {
			repairappFormMap.set("date", sdf.format(date));
			repairappFormMap.set("confirmPeopleId", userFormMap.get("id"));
			repairappFormMap.set("status", 2);
			repairappFormMap.put("id", str[i]);
			repairapp.editEntity(repairappFormMap);
		}
		return "success";
	}
}
